package com.icss.ah.email;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
  
/** 
 * javamail 发送邮件 
 * @author csadchen 
 * Message.addRecipient(Message.Recipient recipient, Address address); 发邮件的时候指定收件人和收件人的角色 
 * Message.RecipientType.TO 收件人 
 * Message.RecipientType.CC 抄送，即发邮件的时候顺便给另一个人抄一份，不用回复！但是，上边的收件人可以看到你都抄送给了谁 
 * Message.RecipientType.BCC 暗送，也是发邮件的时候顺便给另一个人暗发一份，但是，不同于上边的是，收件人不能看到你都暗送给了谁 
 * 
 */
public class Demo2  { 
  
  private static final String sendUserName = "csadchen@163.com";// 发送邮件需要连接的服务器的用户名 
  
  private static final String sendPassword = "cs577087";// 发送邮件需要连接的服务器的密码 
  
  private static final String sendProtocol = "smtp";// 发送邮件使用的端口 
  
  private static final String sendHostAddress = "smtp.163.com";// 发送邮件使用的服务器的地址 
  
  public static void main(String[] args) throws AddressException, MessagingException {
	  test();
  }
  public static void test() throws AddressException, MessagingException { 
  
    Properties properties = new Properties(); 
    properties.setProperty("mail.smtp.auth", "true");// 服务器需要认证 
    properties.setProperty("mail.transport.protocol", sendProtocol);// 声明发送邮件使用的端口 
    properties.setProperty("mail.host", sendHostAddress);// 发送邮件的服务器地址 
  
    Session session = Session.getInstance(properties, new Authenticator() { 
      protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(sendUserName, sendPassword); 
      } 
    }); 
    session.setDebug(true);//在后台打印发送邮件的实时信息 
  
    Message message = new MimeMessage(session); 
    message.setFrom(new InternetAddress(sendUserName)); 
    message.setSubject("邮箱认证");// 设置主题 
    message.setRecipients(Message.RecipientType.TO, InternetAddress 
        .parse("chensheng@chinasofti.com"));// 发送 chensheng@chinasofti.com,csadchen@163.com
//    message.setRecipients(Message.RecipientType.CC, InternetAddress 
//        .parse("413845305@qq.com"));// 抄送 
  
    StringBuffer content = new StringBuffer();
    content.append("<div style='width:800px;'>");
    content.append("	<img src='http://pic.58pic.com/58pic/12/20/97/87U58PICEtK.jpg' style='width:300px;height:80px;'/>");
    content.append("	<div style='padding-left:50px;line-height:35px;font-size:16px;font-weight: 900;WORD-WRAP: break-word;'>");
    content.append("		请确认您的邮箱，只差一步，您的认证就成功了！<span style='color:#D0D0D0'> (请在24小时内完成)：</span>");
    content.append("		<div style='width:750px;text-align:center;padding-top:20px;padding-bottom:20px;'>");
    content.append("			<div style='margin:0 auto;background-color:#FF3300;height:40px;line-height:40px;width:120px;color:#fff'>完成认证</div>");
    content.append("		</div>");
    content.append("		<span style='color:#D0D0D0'>如果亲看不到上方的按钮<br/>可点击下面的链接以完成认证或复制下面的链接到浏览器地址栏中完成认证：</span>");
    content.append("		<div style='WORD-WRAP: break-word;line-height:26px;'>");
    content.append("			<a href='https://passport.alibaba.com/member/request_dispatcher.htm?from=ACTIVE_BY_URL&_ap_action=registerActive&t=ARCAxgoiCFJFR0lTVEVSMgEBOL6I25D8KkABShAdkl4qIbR4RCoCh_Sqjl7rpxkQexc_oIu28YNeUm9ws4l78mY' style=' text-decoration:none;color:#76A1FF'>https://passport.alibaba.com/member/request_dispatcher.htm?from=ACTIVE_BY_URL&_ap_action=registerActive&t=ARCAxgoiCFJFR0lTVEVSMgEBOL6I25D8KkABShAdkl4qIbR4RCoCh_Sqjl7rpxkQexc_oIu28YNeUm9ws4l78mY</a>");
    content.append("		</div>");
    content.append("	</div>");
    content.append("</div>");
    System.out.println("=============content:"+content.toString());
    message.setContent(content.toString(),"text/html;charset=gbk");
    
    Transport.send(message);//发送邮件 
  } 
} 